<template>
  <div class="analysis-card">
    <h3 class="analysis-title">门店分析</h3>
    <div class="metrics-list">
      <!-- 成交金额 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">成交金额</span>
          <span class="amount">¥{{ getData('交易数据-成交金额') }}</span>
        </div>
        <div class="item-value">

          <div class="comparison-data">
            <span :class="getData('交易数据-成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-成交金额-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: ¥{{
              getPreviousValues('交易数据-成交金额',
                '交易数据-成交金额-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>

      <!-- 成交券数 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">成交券数</span>
          <span class="amount">{{ getData('交易数据-成交券数') }}</span>
        </div>
        <div class="item-value">
          <div class="comparison-data">
            <span :class="getData('交易数据-成交券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-成交券数-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: {{
              getPreviousValues('交易数据-成交券数',
                '交易数据-成交券数-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>

      <!-- 核销金额 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">核销金额</span>
          <span class="amount">¥{{ getData('交易数据-核销金额') }}</span>
        </div>
        <div class="item-value">
          <div class="comparison-data">
            <span :class="getData('交易数据-核销金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-核销金额-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: ¥{{
              getPreviousValues('交易数据-核销金额',
                '交易数据-核销金额-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>

      <!-- 核销券数 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">核销券数</span>
          <span class="amount">{{ getData('交易数据-核销券数') }}</span>
        </div>
        <div class="item-value">
          <div class="comparison-data">
            <span :class="getData('交易数据-核销券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-核销券数-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: {{
              getPreviousValues('交易数据-核销券数',
                '交易数据-核销券数-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>

      <!-- 退款金额 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">退款金额</span>
          <span class="amount">¥{{ getData('交易数据-退款金额') }}</span>
        </div>
        <div class="item-value">
          <div class="comparison-data">
            <span :class="getData('交易数据-退款金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-退款金额-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: ¥{{
              getPreviousValues('交易数据-退款金额',
                '交易数据-退款金额-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>

      <!-- 退款券数 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">退款券数</span>
          <span class="amount">{{ getData('交易数据-退款券数') }}</span>
        </div>
        <div class="item-value">
          <div class="comparison-data">
            <span :class="getData('交易数据-退款券数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-退款券数-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: {{
              getPreviousValues('交易数据-退款券数',
                '交易数据-退款券数-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>

      <!-- 退款人数 -->
      <div class="metric-item">
        <div style="display: flex;flex-direction: column">
          <span class="item-label">退款人数</span>
          <span class="amount">{{ getData('交易数据-退款人数') }}</span>
        </div>
        <div class="item-value">
          <div class="comparison-data">
            <span :class="getData('交易数据-退款人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
              {{ getData('交易数据-退款人数-较上周期变化') }}
            </span>
            <span class="previous-value" style="position: absolute;bottom: 5px;right: 10px;">上期: {{
              getPreviousValues('交易数据-退款人数',
                '交易数据-退款人数-较上周期变化')
            }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getPreviousValue } from '../../../utils'
interface ReportItem {
  name: string
  value: string
}

interface ReportSection {
  title: string
  items: ReportItem[]
}

interface Props {
  sectionData: ReportSection
}
const getPreviousValues = (currentKey: string, changeKey: string,): string => {

  const currentValue = getData(currentKey)
  const changeValue = getData(changeKey)
  return getPreviousValue(currentValue, changeValue)
}
const props = defineProps<Props>()

const getData = (key: string): string => {
  const item = props.sectionData.items.find(item => item.name === key)
  return item?.value || '-'
}

</script>

<style scoped>
.analysis-card {
  margin-bottom: 20px;
  padding: 15px 20px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.analysis-title {
  font-size: 18px;
  color: #2d3748;
  font-weight: 600;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid #e2e8f0;
}

.metrics-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.metric-item {
  display: flex;
  padding: 10px;
  justify-content: space-between;
  background: #f8fafc;
  position: relative;
  border-radius: 8px;
  min-width: 140px;
}

.item-label {
  font-size: 13px;
  color: #4a5568;
  margin-bottom: 4px;
}

.item-value {
  display: flex;
  justify-content: space-between;
  gap: 4px;
}

.comparison-data {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.amount {
  font-size: 20px;
  font-weight: 600;
  color: #2d3748;
  position: relative;
  top: -5px;
}

.previous-value {
  font-size: 11px;
  color: #718096;
}

.rate-up,
.rate-down {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
}

.rate-up {
  color: #22c55e;
  background: rgba(34, 197, 94, 0.1);
}

.rate-down {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

@media (max-width: 1024px) {
  .metrics-list {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .metrics-list {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .metrics-list {
    grid-template-columns: 1fr;
  }
}
</style>